
import Vue from 'vue'
import vuex, { Store } from 'vuex'
Vue.use(vuex)
import { nanoid } from 'nanoid'


const helloModule = {
    namespaced: true,
    state: {
        sum: 0,
    },
    actions: {
        oddIncrement(context, value) {
            if (context.state.sum % 2 != 0) {
                context.state.sum += value
            }
        },
        waitIncrement(context, value) {
            setTimeout(() => {
                context.state.sum += value
            }, 2000);
        }
    },
    mutations: {
        INCREMENT(state, value) {
            state.sum += value
        },
        DECREMENT(state, value) {
            state.sum -= value
        },
    },
    getters: {
        bigNum(state) {
            return state.sum * 10
        }
    }
}

const presonModule = {
    namespaced:true,
    state: {
        presonList: [],
    },
    actions: {},
    mutations: {
        ADD_PRESON(state, value) {
            let preson = {
                name: value,
                id: nanoid()

            }
            state.presonList.unshift(preson)
        }
    },
    getters: {}
}


export default new vuex.Store({
   modules:{
    helloModule,
    presonModule
   }
})